
/*错误class  form-control is-invalid
正确class  form-control is-valid*/
var flagPhone=false;
var flagPas=false;
var flagPass=false;
var flagCode=false;
var register_phone = $("#register-phone");
var verification_code = $("#verification-code");
var register_password = $("#register-password");
var register_passwords = $("#register-passwords");
/*验证手机号*/
var name,passWord,passWords;
register_phone.change(function(){
	name=register_phone.val();
	if(name.length!==11){
		register_phone.removeClass("form-control is-valid");
		register_phone.addClass("form-control is-invalid");
		$("#message").text("手机号必须为11位");
		flagPhone=false;
	}
	else if(isPhoneNo($.trim(name)) === false){
		register_phone.removeClass("form-control is-valid");
		register_phone.addClass("form-control is-invalid");
		$("#message").text("手机号格式错误");
		return false
	}else if(isExist(name)){
		//console.log("手机号已注册，请直接登录");
		register_phone.removeClass("form-control is-valid");
		register_phone.addClass("form-control is-invalid");
		$("#message").text("手机号已注册，请直接登录！");
		$("#regbtn").attr("disabled","disabled");
		return false
	}else{
		register_phone.removeClass("form-control is-invalid");
		register_phone.addClass("form-control is-valid");
		flagPhone=true;
	}
});
/*验证验证码是否为4位数*/
verification_code.change(function(){
	name=verification_code.val();
	if(name.length!==4){
		verification_code.removeClass("form-control is-valid");
		verification_code.addClass("form-control is-invalid");
		flagCode=false;
	}else{
		verification_code.removeClass("form-control is-invalid");
		verification_code.addClass("form-control is-valid");
		flagCode=true;
	}
});
/*验证密码*/
register_password.change(function(){
	passWord=register_password.val();
	if(passWord.length<6||passWord.length>18){
		register_password.removeClass("form-control is-valid");
		register_password.addClass("form-control is-invalid");
		flagPas=false;
	}else{
		register_password.removeClass("form-control is-invalid");
		register_password.addClass("form-control is-valid");
		flagPas=true;
	}
});
/*验证确认密码*/
register_passwords.change(function(){
	passWords=register_passwords.val();
	if((passWord!==passWords)||(passWords.length<6||passWords.length>18)){
		register_passwords.removeClass("form-control is-valid");
		register_passwords.addClass("form-control is-invalid");
		flagPass=false;
	}else{
		register_passwords.removeClass("form-control is-invalid");
		register_passwords.addClass("form-control is-valid");
		flagPass=true;
	}
});

//判断手机号是否存在
function isExist(phone) {
	var is = false;
	$.ajax({
		type:"get",
		async:false,
		url:"judgeByPhone",
		data:{
			"phone":register_phone.val()
		},
		success:function (result) {
			if(result===1){
				//console.log("手机已存在");
				is = true;
			}else {
				is = false;
			}

		},
		error:function (e) {

		}
	});
	//console.log(is);
	return is;


}

// 初步验证手机号
function isPhoneNo(phone) {
	var pattern = /^1[34578]\d{9}$/;
	return pattern.test(phone);
}

var sendMes = $("#sendMes");
//验证码发送
sendMes.click(function () {

	var phone = register_phone.val();
	//console.log(phone);

	if(!flagPhone){
		//console.log("手机号有误");
		register_phone.focus();
		return
	}

	//验证倒计时
	var Time = 60;
	var TimeDown = setInterval(timedown,1000);
	function timedown(){
		sendMes.attr("disabled", true);
		sendMes.val(""+ Time + "s");
		if(Time === 0){
			sendMes.val("重新获取").removeAttr("disabled");
			clearInterval(TimeDown);
		}
		Time--;
	}

	$.ajax({
		type:"get",
		url:"send",
		data:{"phone":phone},
		success:function (result) {
			if(result===0){
				//console.log("发送失败");
				flagCode = false;
			}

		},
		error:function (e) {

		}
	})


});



$("#regbtn").click(function(){
	if(flagPhone&&flagPas&&flagPass&&flagCode){

		$.ajax({
			type: "post",
			url: "userRegister",
			data: {
				"phone":register_phone.val(),
				"password":register_password.val(),
				"code":verification_code.val()
			},
			success:function (result) {
				if(result===0){
					$("#error").text("验证码不正确");
					flagCode = false;
					//console.log("验证码不正确");
				}else {
					window.location.href = 'main/index.html';
				}

			},
			error:function (e) {

			}

		})
	}else{
		if(!flagPhone){
			$("#register-phone").addClass("form-control is-invalid");
		}
		if(!flagPas){
			$("#register-password").addClass("form-control is-invalid");
		}
		if(!flagPass){
			$("#register-passwords").addClass("form-control is-invalid");
		}
		if(!flagCode){
			$("#verification-code").addClass("form-control is-invalid");
		}
	}
});

